package com.gnerv.battle.boot.framework.core.tools;

import com.gnerv.battle.boot.framework.core.exception.BusinessException;
import com.google.common.io.Files;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @author Gnerv LiGen
 */
public class FileTools {

    private FileTools() {
    }

    public static void tarDecompress(File file) {
        tarDecompress(file, false);
    }

    public static void tarDecompress(File file, boolean strip) {
        tarDecompress(file, file.getParentFile(), strip);
    }

    public static void tarDecompress(File file, String targetPath) {
        tarDecompress(file, targetPath, false);
    }

    public static void tarDecompress(File file, String targetPath, boolean strip) {
        tarDecompress(file, new File(targetPath), strip);
    }

    public static void tarDecompress(File file, File targetDirectory, boolean strip) {
        if (!file.exists() || !file.isFile()) {
            throw new BusinessException("file not exists or is not a file");
        }
        if (!targetDirectory.isDirectory()) {
            throw new BusinessException("targetDirectory is not a directory");
        }
        if (!targetDirectory.exists()) {
            boolean mkdirs = targetDirectory.mkdirs();
            if (!mkdirs) {
                throw new BusinessException("create target directory failed");
            }
        }
        boolean suffix = suffix(file.getName(), ".tar.gz");
        if (!suffix) {
            throw new BusinessException("it is not a tar.gz file");
        }
        String command;
        if (strip) {
            command = "tar -zxvf " + file.getAbsolutePath() + " -C " + targetDirectory.getAbsolutePath() + " --strip-components 1";
        } else {
            command = "tar -zxvf " + file.getAbsolutePath() + " -C " + targetDirectory.getAbsolutePath();
        }
        try {
            Runtime.getRuntime().exec(command, null, file.getParentFile());
        } catch (IOException e) {
            throw new BusinessException("tar error", e);
        }
    }

    public static void unzip(File file) {
        if (!file.exists() || !file.isFile()) {
            throw new BusinessException("file not exists or is not a file");
        }
        boolean suffix = suffix(file.getName(), ".zip");
        if (!suffix) {
            throw new BusinessException("it is not a zip file");
        }
        String command = "unzip -d " + file.getParentFile().getAbsolutePath() + " " + file.getName();
        try {
            Runtime.getRuntime().exec(command, null, file.getParentFile());
        } catch (IOException e) {
            throw new BusinessException("unzip error", e);
        }
    }

    public static String read(File file) throws IOException {
        return Files.asCharSource(file, StandardCharsets.UTF_8).read();
    }

    public static boolean write(File file, String content) throws IOException {
        Files.write(content.getBytes(StandardCharsets.UTF_8), file);
        return true;
    }

    public static boolean suffix(File file, String suffix) {
        boolean exists = file.exists();
        if (!exists) {
            return false;
        }
        boolean isFile = file.isFile();
        if (!isFile) {
            return false;
        }
        return suffix(file.getName(), suffix);
    }

    public static boolean suffix(String fileName, String suffix) {
        return fileName.endsWith(suffix);
    }

}
